package com.hmy.finance.controller.receipt;


import cn.hutool.core.collection.CollectionUtil;
import com.hmy.ccp.common.response.BasePageResponse;
import com.hmy.ccp.common.result.JsonResult;
import com.hmy.ccp.error.annotations.HmyGateWayController;
import com.hmy.ccp.error.exception.HMYRuntimeException;
import com.hmy.finance.api.bo.core.request.receiptOffset.*;
import com.hmy.finance.api.bo.core.response.receiptOffset.*;
import com.hmy.finance.service.receipt.ReceiptOffsetApprovalService;
import com.hmy.finance.service.receipt.ReceiptOffsetBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

@Api(tags = "收款核销")
@RestController
@RequestMapping("/api/receiptOffset")
@Validated
@Slf4j
@HmyGateWayController
public class ReceiptOffsetController {

    @Resource
    private ReceiptOffsetBillService receiptOffsetBillService;

    @Resource
    private ReceiptOffsetApprovalService receiptOffsetApprovalService;


    @ApiOperation("boss端分页查询核销单")
    @PostMapping("/queryReceiptOffsetBillPageInfosByBoss")
    public JsonResult<BasePageResponse<ReceiptOffsetBillResponse>> queryReceiptOffsetBillPageInfosByBoss(@RequestBody @Validated ReceiptOffsetBillBossPageQueryRequest receiptOffsetBillBossPageQueryRequest) {
        BasePageResponse<ReceiptOffsetBillResponse> response = receiptOffsetBillService.queryReceiptOffsetBillPageInfosByBoss(receiptOffsetBillBossPageQueryRequest);
        return JsonResult.success(response);
    }

    @ApiOperation("app端分页查询核销单")
    @PostMapping("/queryReceiptOffsetBillPageInfosByApp")
    public JsonResult<BasePageResponse<ReceiptOffsetBillAppPageQueryResponse>> queryReceiptOffsetBillPageInfosByApp(@RequestBody @Validated ReceiptOffsetBillAppPageQueryRequest receiptOffsetBillAppPageQueryRequest) {
        BasePageResponse<ReceiptOffsetBillAppPageQueryResponse> response = receiptOffsetBillService.queryReceiptOffsetBillPageInfosByApp(receiptOffsetBillAppPageQueryRequest);
        return JsonResult.success(response);
    }

    @ApiOperation("查询核销单详情")
    @PostMapping("/queryReceiptOffsetBillDetail")
    public JsonResult<ReceiptOffsetBillDetailResponse> queryReceiptOffsetBillDetail(@RequestBody @Validated ReceiptOffsetBillDetailQueryRequest receiptOffsetBillDetailQueryRequest) {
        if (Objects.isNull(receiptOffsetBillDetailQueryRequest.getReceiptOffsetBillId()) && StringUtils.isEmpty(receiptOffsetBillDetailQueryRequest.getReceiptOffsetBillNo())) {
            throw new HMYRuntimeException(506800021);
        }
        ReceiptOffsetBillDetailResponse response = receiptOffsetBillService.queryReceiptOffsetBillDetail(receiptOffsetBillDetailQueryRequest);
        return JsonResult.success(response);
    }

    @ApiOperation("查询审核中的核销单")
    @PostMapping("/queryAuditingReceiptOffsetBills")
    public JsonResult<List<AuditingReceiptOffsetBillQueryResponse>> queryAuditingReceiptOffsetBills(@RequestBody @Validated AuditingReceiptOffsetBillCountQueryRequest auditingReceiptOffsetBillCountQueryRequest) {
        List<AuditingReceiptOffsetBillQueryResponse> response = receiptOffsetBillService.queryAuditingReceiptOffsetBills(auditingReceiptOffsetBillCountQueryRequest);
        return JsonResult.success(response);
    }

    @ApiOperation("创建核销单")
    @PostMapping("/createReceiptOffsetBill")
    public JsonResult<ReceiptOffsetBillCreateResponse> createReceiptOffsetBill(@RequestBody @Validated ReceiptOffsetBillCreateRequest receiptOffsetBillCreateRequest) {
        if (CollectionUtil.isNotEmpty(receiptOffsetBillCreateRequest.getContactorApportionInfos())) {
            BigDecimal totalApportionAmount = receiptOffsetBillCreateRequest.getContactorApportionInfos().stream().map(ContactorApportionRequest::getApportionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            if (receiptOffsetBillCreateRequest.getCustomerOffsetAmount().compareTo(totalApportionAmount) < 0) {
                throw new HMYRuntimeException(506800022);
            }

            receiptOffsetBillCreateRequest.getContactorApportionInfos().forEach(c -> {
                BigDecimal totalContractApportionAmount = c.getContractApportions().stream().map(ContractApportionRequest::getContractActualOffsetAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                if (c.getApportionAmount().compareTo(totalContractApportionAmount) < 0) {
                    throw new HMYRuntimeException(506800023, new Object[]{c.getCustomerContactorName()});
                }
            });
        }
        ReceiptOffsetBillCreateResponse response = receiptOffsetBillService.createReceiptOffsetBill(receiptOffsetBillCreateRequest);
        return JsonResult.success(response);
    }

    @ApiOperation("编辑核销单")
    @PostMapping("/editReceiptOffsetBill")
    public JsonResult<ReceiptOffsetBillEditResponse> editReceiptOffsetBill(@RequestBody @Validated ReceiptOffsetBillEditRequest receiptOffsetBillEditRequest) {
        if (CollectionUtil.isNotEmpty(receiptOffsetBillEditRequest.getContactorApportionInfos())) {
            BigDecimal totalApportionAmount = receiptOffsetBillEditRequest.getContactorApportionInfos().stream().map(ContactorApportionRequest::getApportionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            if (receiptOffsetBillEditRequest.getCustomerOffsetAmount().compareTo(totalApportionAmount) < 0) {
                throw new HMYRuntimeException(506800022);
            }
            receiptOffsetBillEditRequest.getContactorApportionInfos().forEach(c -> {
                BigDecimal totalContractApportionAmount = c.getContractApportions().stream().map(ContractApportionRequest::getContractActualOffsetAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                if (c.getApportionAmount().compareTo(totalContractApportionAmount) < 0) {
                    throw new HMYRuntimeException(506800023, new Object[]{c.getCustomerContactorName()});
                }
            });
        }
        ReceiptOffsetBillEditResponse response = receiptOffsetBillService.editReceiptOffsetBill(receiptOffsetBillEditRequest);
        return JsonResult.success(response);
    }

    @ApiOperation("删除核销单")
    @PostMapping("/deleteReceiptOffsetBill")
    public JsonResult<ReceiptOffsetBillDeleteResponse> deleteReceiptOffsetBill(@RequestBody @Validated ReceiptOffsetBillDeleteRequest receiptOffsetBillDeleteRequest) {
        ReceiptOffsetBillDeleteResponse response = receiptOffsetBillService.deleteReceiptOffsetBill(receiptOffsetBillDeleteRequest);
        return JsonResult.success(response);
    }

    @ApiOperation("查询指定客户+联系人下收款核销方案")
    @PostMapping("/queryContactorApportions")
    public JsonResult<ContactorApportionsQueryResponse> queryContactorApportions(@RequestBody @Validated ContactorApportionsQueryRequest contactorApportionsQueryRequest) {
        ContactorApportionsQueryResponse response = receiptOffsetBillService.queryContactorApportions(contactorApportionsQueryRequest);
        return JsonResult.success(response);
    }

    @ApiOperation("获取核销方式类型")
    @PostMapping("/getReceiptedTypes")
    public JsonResult<List<ReceiptedTypeEnumResponse>> getReceiptedTypes() {
        List<ReceiptedTypeEnumResponse> response = receiptOffsetBillService.getReceiptedTypeEnums();
        return JsonResult.success(response);
    }


    @ApiOperation("撤销收款核销申请")
    @PostMapping("/revokeReceiptOffset")
    public JsonResult<ReceiptOffsetRevokeResponse> revokeReceiptOffset(@RequestBody @Validated ReceiptOffsetRevokeRequest receiptOffsetRevokeRequest) {
        ReceiptOffsetRevokeResponse response = receiptOffsetApprovalService.revokeReceiptOffset(receiptOffsetRevokeRequest);
        return JsonResult.success(response);
    }

    @ApiOperation("作废收款核销单申请")
    @PostMapping("/cancelReceiptOffset")
    public JsonResult<ReceiptOffsetCancelResponse> cancelReceiptOffset(@RequestBody @Validated ReceiptOffsetCancelRequest receiptOffsetCancelRequest) {
        ReceiptOffsetCancelResponse response = receiptOffsetApprovalService.cancelReceiptOffset(receiptOffsetCancelRequest);
        return JsonResult.success(response);
    }

    @ApiOperation("审批通过/驳回收款核销申请（聚合所有审核节点）")
    @PostMapping("/auditReceiptOffset")
    public JsonResult<ReceiptOffsetAuditResponse> auditReceiptOffset(@RequestBody @Validated ReceiptOffsetAuditRequest receiptOffsetAuditRequest) {
        ReceiptOffsetAuditResponse response = receiptOffsetApprovalService.auditReceiptOffset(receiptOffsetAuditRequest);
        return JsonResult.success(response);
    }

    @ApiOperation("退回收款核销申请")
    @PostMapping("/retractReceiptOffset")
    public JsonResult<ReceiptOffsetRetractResponse> retractReceiptOffset(@RequestBody @Validated ReceiptOffsetRetractRequest receiptOffsetRetractRequest) {
        ReceiptOffsetRetractResponse response = receiptOffsetApprovalService.retractReceiptOffset(receiptOffsetRetractRequest);
        return JsonResult.success(response);
    }

}
